﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;
using System.Text;

namespace Com.CMS263.Web.manage.ProdAttr
{
    public partial class EditProdAttr : System.Web.UI.Page
    {
        public ProdAttribute pro = new ProdAttribute();
        //ptid="+ptid+"&id="+id+"&page="+page;
        public ProdAttributeBLL probll = new ProdAttributeBLL();
        public  int page = 0;
        int ptid = 0;
        int id = 0;
        public string mo = string.Empty;
        public int atr = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            ptid = Convert.ToInt32(Request["ptid"]);
            id = Convert.ToInt32(Request["id"]);
            page = Convert.ToInt32(Request["page"]);
            if (!IsPostBack)
            {
                Bind();
            }

        }

        public void Bind()
        {
            pro = probll.GetPrById(id);
            ProdType pt = new ProdType();
            ProdTypeBLL ptbll = new ProdTypeBLL();
            atr = pro.AttrType;
            pt = ptbll.GetProdTypeById(ptid);
            lblTypeName.Text = pt.TypeName;
            txtAttrName.Text = pro.AttrName;
            txtAttrEnName.Text = pro.AttrEnName;
            if (pro.AttrType >= 6 && pro.AttrType <= 8)
            {
                ddlType.Enabled = true;
                ddlType.SelectedValue = pro.AttrType.ToString ();
                txtAttrValue.Enabled = true;
            }
            else
            {
                ddlType.Enabled = false;
                ddlType.SelectedValue = pro.AttrType.ToString();
            }

            string atv = pro.AttrValue;
            string mm = atv.Replace(" ","\r\n");

            txtAttrValue.Text = mm;
            chbIsNull.Checked = pro.IsNull;
            chbShowBack.Checked = pro.IsBackShow;
            txtDefaultValue.Text = pro.DefaultValue;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ProdAttribute prod = new ProdAttribute();
            string attrname = txtAttrName.Text;
            string attrenname = txtAttrEnName.Text;
            string attrvalue = txtAttrValue.Text;
            string defaultvalue = txtDefaultValue.Text;
            
            if (string.IsNullOrEmpty(attrname))
            {
                StringUtil.Alert("不能为空！","-1");
            }
            else
            {
                attrname = StringUtil.checkStr(attrname);
            }
            if (string.IsNullOrEmpty(attrenname))
            {
                StringUtil.Alert("不能为空！", "-1");
            }
            else
            {
                attrenname = StringUtil.checkStr(attrenname);
            }
            if (!string.IsNullOrEmpty(attrvalue))
            {
                attrvalue = StringUtil.checkStr(attrvalue);
            }
            else
            {
                attrvalue = "";
            }
            if (!string.IsNullOrEmpty(defaultvalue))
            {
                defaultvalue = StringUtil.checkStr(defaultvalue);
            }
            else
            {
                defaultvalue = "";   
            }
            if (ddlType.SelectedValue == "0")
            {
                StringUtil.Alert("请选择属性类型!", "-1");
            }
            else
            {
                prod.AttrType = Convert.ToInt32(ddlType.SelectedValue);
            }
            prod.AttrName = attrname;
            prod.AttrEnName = attrenname;
            prod.AttrValue = attrvalue;
            prod.DefaultValue = defaultvalue;
            prod.IsNull = chbIsNull.Checked;
            prod.IsBackShow = chbShowBack.Checked;
            prod.Id =Convert .ToInt32 ( Request["iid"]);
            prod.ProdTypeId = Convert.ToInt32(Request["ptid"]);
            probll.entity = prod;
            bool b = probll.UpDate();
            if (b)
            {
                StringUtil.Alert("修改成功！", "ProdTypeAtt.aspx?ptid=" + Request["ptid"] + "&page=" + page);
            }
            else
            {
                StringUtil.Alert("修改失败！", "-1");
            }

            
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
           
        }
    }
}
